package com.it.myh;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: Od_0221_04    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2023/2/21 16:18   // 时间
 * @Version: 1.0     // 版本
 */
public class Od_0221_04 {

    //客户端
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;

        try {
            //1.获取IP地址
            InetAddress localHost = InetAddress.getLocalHost();
            //2.创建socket对象，指明服务端的ip和端口号
            socket = new Socket(localHost,8899);
            //3.通过socket获取一个输出流，用于输出数据
            outputStream = socket.getOutputStream();
            //4.写出数据的操作
            outputStream.write("我是客户端前台".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关流
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
